package com.freerdp.afreerdp.presentation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.freerdp.afreerdp.R;
import com.freerdp.afreerdp.application.GlobalApp;
import com.freerdp.afreerdp.domain.BookmarkBase;
import com.freerdp.afreerdp.domain.ConnectionReference;
import com.freerdp.afreerdp.domain.ManualBookmark;
import com.freerdp.afreerdp.services.BookmarkBaseGateway;
import com.freerdp.afreerdp.utils.ActivityStackControlUtil;

public class MainActivity extends Activity{

	private BookmarkBase bookmark = null;
	
	private Button home_button;	
	private Button connect_button;
	private Button sreenWH_button;
	private Button setWH_button;
	
	private TextView sreenWHTextView;
	
	private TextView setWHTextView;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		setTitle("重资集团");		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		
		ActivityStackControlUtil.add(this);
		
		initListener();		
		connect();
	}
	
	private void initListener(){
		
		home_button = (Button)findViewById(R.id.home_button);
		home_button.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View view) {
            	Intent sessionIntent = new Intent(view.getContext(), HomeActivity.class);					
				startActivity(sessionIntent);
            }
        });
		
		connect_button = (Button)findViewById(R.id.connect_button);
		connect_button.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v) {
            	connect();
            }
        });
		
		sreenWHTextView = (TextView) findViewById(R.id.sreenWHTextViewId);
		
		sreenWH_button = (Button)findViewById(R.id.sreenWH_button);
		sreenWH_button.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v) {
            	DisplayMetrics dm = new DisplayMetrics();  
            	getWindowManager().getDefaultDisplay().getMetrics(dm);
            	Log.i("__bookmark__", "heigth : " + dm.heightPixels);
            	Log.i("__bookmark__", "width : " + dm.widthPixels);
            	
            	String hwStr = "heigth : " + dm.heightPixels+",width : " + dm.widthPixels;
            	sreenWHTextView.setText(hwStr);
            }
        });
		
		setWHTextView = (TextView) findViewById(R.id.setWHTextViewId);
		
		setWH_button = (Button)findViewById(R.id.setWH_button);
		setWH_button.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v) {
            	
            	String refStr = "MBMID/1";
        		bookmark = GlobalApp.getManualBookmarkGateway().findById(ConnectionReference.getManualBookmarkId(refStr));
        		BookmarkBase.ScreenSettings setting =bookmark.getAdvancedSettings().getScreen3G();
        		       		
        		String hwStr = "实际传入参数heigth : " + setting.getHeight()+",width : " + setting.getWidth();
        		setWHTextView.setText(hwStr);
            }
        });
	}
	
	private void connect(){
		String refStr = "MBMID/1";
		bookmark = GlobalApp.getManualBookmarkGateway().findById(ConnectionReference.getManualBookmarkId(refStr));
		//判断有没有
		//有，就取出来;没有就创建 插入
		if(bookmark==null){
			Log.i("__bookmark__", "没有bookmark");
			bookmark = new ManualBookmark();
			bookmark.<ManualBookmark>get().setPort(6003);
			bookmark.<ManualBookmark>get().setHostname("202.107.200.134");
			
			// "custom" "width" "heigth"
			DisplayMetrics dm = new DisplayMetrics();  
        	getWindowManager().getDefaultDisplay().getMetrics(dm);
			bookmark.getAdvancedSettings().getScreen3G().setResolution("custom",dm.widthPixels,dm.heightPixels);
			bookmark.getScreenSettings().setResolution("custom",dm.widthPixels,dm.heightPixels);
			
			bookmark.readFromSharedPreferencesZ6(getSharedPreferences("TEMP", 0));
			BookmarkBaseGateway bookmarkGateway =  GlobalApp.getManualBookmarkGateway();
			bookmarkGateway.insert(bookmark);
			Log.i("__bookmark__", "插入成功bookmark");
		}else{
			Log.i("__bookmark__", "有bookmark Label="+bookmark.getLabel()+","+bookmark.<ManualBookmark>get().getHostname());
			//BookmarkBase.ScreenSettings setting =bookmark.getAdvancedSettings().getScreen3G();
			//Log.i("__bookmark__", "有bookmark ="+setting.getResolutionString()+","+setting.getResolutionString());
			//Log.i("__bookmark__", "有bookmark width="+setting.getWidth());
			//Log.i("__bookmark__", "有bookmark height="+setting.getHeight());
		}
		
		Bundle bundle = new Bundle();
		bundle.putString(SessionActivity.PARAM_CONNECTION_REFERENCE, refStr);				

		Intent sessionIntent = new Intent(getApplicationContext(), SessionActivity.class);				
		sessionIntent.putExtras(bundle);
		startActivity(sessionIntent);
		
	}
	
}
